<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;
		var $smarty	= null;

		function source(&$framework, &$security)
		{

			$this->fw		= &$framework;
			$this->sec		= &$security;

			$this->smarty	= new smarty;

			// Set Smarty Paths
			$this->smarty->template_dir = _BASE_PATH .'/templates/'. $this->fw->config['smarty_template'] .'/';
			$this->smarty->plugins_dir[] =_BASE_PATH .'/includes/classes/smarty_plugins/';

			switch($this->fw->do)
			{

				case 'clear':
					$this->clear_list();
					break;

				case 'remove':
					$this->remove_list();
					break;

				case 'add':
					$this->add_list();
					break;

				default:
					$this->display_list();
					break;

			}

			// Because this is a popup, we have our own smarty object!
			$this->fw->content['msg'] = isset($_SESSION['msg']) ? $_SESSION['msg'] : false;
			unset($_SESSION['msg']);

			$this->smarty->assign('config', $this->fw->config);

			$this->smarty->assign('authenticated', $this->sec->auth);
			$this->smarty->assign('template', $this->fw->template);
			$this->smarty->assign('content', $this->fw->content);

			$this->smarty->display('list.template.tpl');

			exit;

		}

		function add_list()
		{

			$id	= $_REQUEST['id'];
			$id	= is_array($id) ? $id : array($id);

			// Store the selected movie_ids, in an array
			foreach($id as $v)
			{

				$_SESSION['list'][] = (int)$v;
			}

			// Incase a movie has been added twice, make sure all the values are unique
			$_SESSION['list'] = array_unique($_SESSION['list']);

			header('location: index.php?what=list');

		}

		function clear_list()
		{

			unset($_SESSION['list']);

			$_SESSION['msg']	= 'Custom Movie List Cleared';
			header('location: index.php?what=list');

		}

		function display_list()
		{

			$movie_id	= isset($_SESSION['list']) ? $_SESSION['list'] : false;

			if($movie_id !== false)
			{

				$ids	= implode(',', $movie_id);
				$sql	= $this->fw->_pq("SELECT movie_id, movie_title, movie_imdb, movie_year FROM #__movies WHERE movie_id IN (%s)", $ids);
				$result	= mysql_query($sql) or die(mysql_error());

				while($row = mysql_fetch_array($result))
				{

					$results[]	= array('id'			=> $row['movie_id'],
										'title'			=> $row['movie_title'],
										'year'			=> $row['movie_year'],
										'imdb'			=> $row['movie_imdb'],
										'imdb_url'		=> $this->fw->imdb_url($row['movie_imdb']));

				}

				$this->fw->content['movies']	= isset($results) ? $results : false;

			}

		}

		function remove_list()
		{

			$id	= $_REQUEST['id'];
			$id	= is_array($id) ? $id : array($id);

			foreach($id as $v) {
				unset($_SESSION['list'][array_search($v, $_SESSION['list'])]);
			}

			// if there are no movies left in the selection, remove this session var
			if(count($_SESSION['list']) == 0) {
				unset($_SESSION['list']);
			}

			$_SESSION['msg']	= count($id) .' Movie(s) removed';
			header('location: index.php?what=list');

		}

	}

?>